<?php 

define("FETCH_MODE_NONE", -1);
define("FETCH_MODE_CELL",0);
define("FETCH_MODE_ROW",1);
define("FETCH_MODE_COLUMN",2);
define("FETCH_MODE_TABLE",3);
define("DEFAULT_LANGUAGE",'vn');
define("DEFAULT_PAGE",'trang-chu');
define("ROW_PER_PAGE",'10');
define("PAGE_QUOTE","<table width='100%' cellspacing='0' cellpadding='0' border='0'>
	<tr>
		<th valign='top'>[Hình minh họa]</th>
		<td valign='top'>[Nội dung trích dẫn]</td>
	</tr>
</table>");

global $page;
global $mode;
global $language;
global $login;

class Db
{
	private static $_link;
	
	static function mysql_query($query,$fetch_mode=2)
	{
		Db::mysql_connect();
		$result = mysql_query($query);
		if (!$result) 
		{
			die('Invalid query: ' . mysql_error());
		}
		switch($fetch_mode)
		{
			case FETCH_MODE_ROW:
				if($row=mysql_fetch_assoc($result))
					return $row;
				break;
			case FETCH_MODE_CELL:
				if($row=mysql_fetch_row($result))
					return $row[0];						
				break;
			case FETCH_MODE_TABLE:
				$table=array();
				while($row=mysql_fetch_assoc($result))
					$table[]=$row;
				return $table;
				break;
			case FETCH_MODE_COLUMN:
				$array=array();
				while($row=mysql_fetch_row($result))
					$array[]=$row[0];
				return $array;
				break;
			case FETCH_MODE_NONE:
				return true;
				break;
		}
		return null;
	}
	
	static function mysql_connect()
	{
		if(Db::$_link==null)
		{
			$link = mysql_connect(SVR, USN, PWD);
			if (!$link) 
			{
				die('Could not connect: ' . mysql_error());
			}
			$db_selected = mysql_select_db(DTB, $link);
			if (!$db_selected) 
			{
				die ('Can\'t use db : ' . mysql_error());
			}
			mysql_set_charset('utf8',$link); 
			Db::$_link=$link;
		}
	}
	
	static function mysql_close()
	{
		mysql_close(Db::$_link);
	}
}

class Partial
{
	static public function create_menu_item($items,$group_id)
	{
		$string='';
		foreach($items as $item)
		{
			$string.="
			<li>
				<a style='margin-left:48px' href='index.php?page=".$item['page_name']."&id=".$item['page_id']."&group_id=$group_id'>
					<span>".$item['page_title']."</span>
				</a>
			</li>
			<li class='update'>
				<a  href='javascript:updateItem(".$item['page_id'].",$group_id)' title='Cập nhật mục tin này'>
					<span class='process'>[!]</span>
				</a>
			</li>
			<li class='delete'>
				<a  href='javascript:deleteItem(".$item['page_id'].")' title='Xóa mục tin này'>
					<span class='process'>[x]</span>
				</a>
			</li>";
		}
		return $string;
	}
	static public function create_client_menu_item($items,$group_id)
	{
		$string='';
		foreach($items as $item)
		{
			$string.="
			<li>
				<a href='index.php?page=".$item['page_name']."&id=".$item['page_id']."&group_id=$group_id'>
					<span>".$item['page_title']."</span>
				</a>
			</li>";
		}
		return $string;
	}
}

class Page
{
	static public function save_page_content_by_name($page_name,$page_content)
	{
		global $language;
		$column='page_content_'.$language;
		$where='  1';
		$page_content=stripslashes($page_content);
		$query="UPDATE tbl_page 
		SET $column='$page_content' 
		WHERE page_name='$page_name'";
		return Db::mysql_query($query,FETCH_MODE_NONE);
	}
	
	static public function format_page_content($page_content,$page_type)
	{
		switch ($page_type)
		{
			case 0:
				$page_content=nl2br($page_content);
				break;
			case 1:
				break;
		}
		return $page_content;
	}
	
	static public function save_page_content_by_id($page_id,$page_content,$page_title,$page_quote="",$page_sent=0,$page_focus=0)
	{
		global $language;
		$column='page_content_'.$language;
		$page_content=stripslashes($page_content);
		$page_name=Common::no_mark($page_title);
		$query="UPDATE tbl_page 
		SET $column='$page_content',page_title='$page_title',page_name='$page_name',page_focus='$page_focus',page_sent='$page_sent',page_quote_$language='$page_quote',page_status=1
		WHERE page_id='$page_id'"; 
		Db::mysql_query($query,FETCH_MODE_NONE);
		return $page_name;
	}
	
	static public function fetch_page_by_id($page_id)
	{
		global $language;
		$column='page_content_'.$language;
		$query="SELECT * FROM tbl_page WHERE page_id='$page_id' ";
		$row=Db::mysql_query($query,FETCH_MODE_ROW);
		if($row)
			$row['page_content']=$row[$column];
		return $row;
	}
	
	static public function fetch_page_by_name($page_name)
	{
		global $language;
		$column='page_content_'.$language;
		$query="SELECT * FROM tbl_page WHERE page_name='$page_name' ";
		$row=Db::mysql_query($query,FETCH_MODE_ROW);
		if($row)
			$row['page_content']=$row[$column];
		return $row;
	}
	
	//use for page_group_id=0
	static public function fetch_page_content_by_name($page_name)
	{
		$row=Page::fetch_page_by_name($page_name);
		$page_content='';
		if($row!=null)
		{
			$page_type=$row['page_type'];
			$page_content=$row['page_content'];
			$page_content=Page::format_page_content($page_content,$page_type);	
		}
		return $page_content;
	}
	
	static public function fetch_page_title_in_db($page_name)
	{
		$row=Page::fetch_page_by_name($page_name);
		$page_title='';
		if($row!=null)
			$page_title=$row['page_title'];
		else
			$row=Page::fetch_page_by_name($page_name);
		return $page_title;
	}
	
	static public function fetch_page_title_in_constant($page_name)
	{
		$tmp_page_name=$page_name;
		if($tmp_page_name[0]=='_')
			$tmp_page_name=substr($tmp_page_name,1);
		$tmp_page_name=str_replace('-','_',$tmp_page_name);
		$tmp_page_name="L".strtoupper($tmp_page_name);
		$page_title='';
		eval("if(defined('$tmp_page_name')) {\$page_title=$tmp_page_name;}");
		return $page_title;
	}
	
	static public function fetch_page_title($page_name)
	{
		$page_title=Page::fetch_page_title_in_constant($page_name);
		if($page_title=='')
			$page_title=Page::fetch_page_title_in_db($page_name);
		return $page_title;
	}
	
	static public function fetch_pages_by_group_id($page_group_id)
	{
		$query="SELECT * FROM tbl_page WHERE page_group_id='$page_group_id' AND page_status=1";
		return Db::mysql_query($query,FETCH_MODE_TABLE);
	}
}

class Common
{	
	static function check_auth()
	{
		global $login;
		global $page;
		$check_pages=array('_change-password','_delete','_insert','_mode','_update-by-id','_update-by-name','_update-class','_update-news');
		if($login==0 && in_array($page,$check_pages))
		{
			Common::header_location(null,'index.php?page=_login');
			return 0;
		}
		return 1;
	}
	static function header_location($message=null,$url=null,$script_afer_alert="")
	{
		if($url==null)
			$url_script='';
		else if(is_numeric($url))
			$url_script="history.go($url);";
		else
			$url_script="window.location='$url';" ;
		$message_script=$message==null?'':"alert('$message');";		
		if($url==null)
		{
			$script="<script>$(document).ready(function(){ $message_script $script_afer_alert});</script>";
			echo ($script);
		}
		else 
		{
			$script="<script> $message_script $url_script </script>";
			die($script);
		}
	}
	
	static function no_mark($string,$separator='',$replace_char='')
	{
		$separator=$separator==''?'-':$separator;
		$replace_char=$replace_char==''?' ':$replace_char;
		if ($string != '')
		{
			$string = trim ( $string );
			$string = preg_replace ( '/\s\s+/', ' ', $string );
			$string = str_replace ( array ("\r\n", "\r", "\t", "\n" ), ' ', $string );
			$string = mb_convert_case ( $string, MB_CASE_LOWER, 'utf-8' );
			$chars = array ('a' => array ('a', 'á', 'à', 'ả', 'ạ', 'ã', 'â', 'ấ', 'ầ', 'ẩ', 'ậ', 'ẫ', 'ă', 'ắ', 'ằ', 'ẳ', 'ặ', 'ẵ' ), 
						'b' => array ('b' ), 
						'c' => array ('c' ), 
						'd' => array ('d', 'đ' ), 
						'e' => array ('e', 'é', 'è', 'ẻ', 'ẹ', 'ẽ', 'ê', 'ế', 'ề', 'ể', 'ệ', 'ễ' ), 
						'f' => array ('f' ), 
						'g' => array ('g' ), 
						'h' => array ('h' ), 
						'j' => array ('j' ), 
						'k' => array ('k' ), 
						'l' => array ('l' ), 
						'm' => array ('m' ), 
						'n' => array ('n' ), 
						'i' => array ('i', 'í', 'ì', 'ỉ', 'ị', 'ĩ' ), 
						'o' => array ('o', 'ó', 'ò', 'ỏ', 'ọ', 'õ', 'ơ', 'ớ', 'ờ', 'ở', 'ợ', 'ỡ', 'ô', 'ố', 'ồ', 'ổ', 'ộ', 'ỗ' ), 
						'u' => array ('u', 'ú', 'ù', 'ủ', 'ụ', 'ũ', 'ư', 'ư', 'ừ', 'ử', 'ự', 'ữ' ), 
						'y' => array ('y', 'ý', 'ỳ', 'ỵ', 'ỷ', 'ỹ' ), 
						'q' => array ('q' ), 
						'w' => array ('w' ), 
						'r' => array ('r' ), 
						't' => array ('t' ), 
						'p' => array ('p' ), 
						's' => array ('s' ), 
						'z' => array ('z' ), 
						'x' => array ('x' ), 
						'v' => array ('v' ) );
			foreach ( $chars as $k => $v )
				$string = str_replace ( $v, $k, $string );
			if ($separator != '')
				$string = str_replace ( $replace_char, $separator, $string );
			$string = preg_replace ( '/[^a-z0-9\s-.]/i', NULL, $string );
		}
		return $string;
	}
	
	static function init()
	{	
		//
		global $page;
		global $mode;
		global $language;
		global $login;
		
		//
		require_once "ext/php/config.php";
		
		//
		session_start();
		if(!isset($_SESSION['base_url']))
			$_SESSION['base_url']=BASE_URL;
		if(!isset($_SESSION['language']))
			$_SESSION['language']=DEFAULT_LANGUAGE;
		if(!isset($_SESSION['login']))
			$_SESSION['login']=0;
		if(!isset($_SESSION['mode']))		
			$_SESSION['mode']='view';
		$login=$_SESSION['login'];
		$language=$_SESSION['language'];
		$mode=$_SESSION['mode'];
		$base_url=$_SESSION['base_url'];
		
		//
		require_once "ext/php/".$language.".php";
		
		//
		$page=isset($_GET['page'])?$_GET['page']:DEFAULT_PAGE;
	}
}

Common::init();
?>


